package com.un.ebs.businessObject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.AESTool;
import com.un.ebs.core.util.StringUtils;
import com.un.ebs.sys.domain.DataSource;
import com.un.ebs.sys.mapper.DataSourceMapper;
import com.un.ebs.businessObject.service.DataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author aikun
 * 服务实现类
 * @ClassName DataSourceServiceImpl
 * @Description DataSourceServiceImpl
 * @Date 2020/1/8
 */
@Service
public class DataSourceServiceImpl extends ServiceImpl<DataSourceMapper, DataSource> implements DataSourceService {
    @Autowired
    private DataSourceMapper dataSourceMapper;

    @Override
    public void add(DataSource dataSource) {
        if (!StringUtils.isNullOrEmpty(dataSource.getPassword())) {
            dataSource.setPassword(AESTool.Encrypt(dataSource.getPassword()));
        }
        if (!StringUtils.isNullOrEmpty(dataSource.getIpAddr())) {
            dataSource.setIpAddr(AESTool.Encrypt(dataSource.getIpAddr()));
        }
        LambdaQueryWrapper<DataSource> lq = Wrappers.lambdaQuery();
        lq.eq(DataSource::getName, dataSource.getName());
        DataSource dataSource1 = dataSourceMapper.selectOne(lq);
        if (dataSource1 != null) {
            throw new BusinessException("数据源名称重复");
        }
        dataSourceMapper.insert(dataSource);
    }

    @Override
    public void update(DataSource dataSource) {
//        if (!StringUtils.isNullOrEmpty(dataSource.getPassword())) {
//            dataSource.setPassword(AESTool.Encrypt(dataSource.getPassword()));
//        }
//        if (!StringUtils.isNullOrEmpty(dataSource.getIpAddr())) {
//            dataSource.setIpAddr(AESTool.Encrypt(dataSource.getIpAddr()));
//        }
//        LambdaQueryWrapper<DataSource> lq = Wrappers.lambdaQuery();
//        lq.eq(DataSource::getName, dataSource.getName());
//        DataSource dataSource1 = dataSourceMapper.selectOne(lq);
//        if (dataSource1 != null && !dataSource.getId().equals(dataSource1.getId())) {
//            throw new BusinessException("数据源名称重复");
//        }
//        dataSource.updateById();
//        dataSourceMapper.updateById(dataSource);
        dataSource.updateById();
    }

    @Override
    public DataSource getById(String id) {
        DataSource dataSource = dataSourceMapper.selectById(id);
        if (dataSource == null) {
            throw new BusinessException("数据源不存在");
        }
        dataSource.setIpAddr(dataSource.getIpAddr());
        dataSource.setPassword(dataSource.getPassword());
        return dataSource;
    }

    @Override
    public void delete(String id) {
        dataSourceMapper.deleteById(id);
    }

    @Override
    public IPage<DataSource> list(Integer dbType, String userName, String name, int pageSize, int pageIndex) {
        Page<DataSource> page = new Page<>(pageIndex, pageSize);
        LambdaQueryWrapper<DataSource> lq = Wrappers.lambdaQuery();
        if (dbType != null) {
            lq.eq(DataSource::getDbType, dbType);
        }
        if (!StringUtils.isNullOrEmpty(name)) {
            lq.like(DataSource::getName, name);
        }
        if (!StringUtils.isNullOrEmpty(userName)) {
            lq.like(DataSource::getUserName, userName);
        }
        IPage<DataSource> iPage = dataSourceMapper.selectPage(page, lq);
        //解密url,密码
        List<DataSource> dataSourceList = iPage.getRecords();
        if (!StringUtils.isNullOrEmpty(dataSourceList)) {
            for (DataSource dataSource : dataSourceList) {
                dataSource.setIpAddr(AESTool.Decrypt(dataSource.getIpAddr()));
                dataSource.setPassword(AESTool.Decrypt(dataSource.getPassword()));
            }
        }
        return iPage;
    }

}
